<template>
  <div class="detail">
    <detail-nav-bar @titleClick="titleClick" ref="detailNav"/>

    <scroll class="detail-content" :probe-type="3" @scroll="contentScroll" ref="scroll">
      <a-carousel autoplay>
        <div v-for="item in topImages">
          <img :src="item" @load="carLoad"/>
        </div>
      </a-carousel>

      <detail-info :goods="goods"/>

      <detail-shop :shop="shop"/>

      <div class="white"></div>

      <goods-info :detail-info="detailInfo" @goodsImgLoad="goodsImgLoad"/>

      <detail-params-info ref="params" :item-params="itemParams"/>

      <detail-comment-info ref="comment" :comment-info="commentInfo"/>

      <goods-list ref="recommend" :goods="recommends"/>


      <!--      <div v-for="item in 10">{{item}}</div>-->
    </scroll>

    <back-top @click.native="backClick" v-show="isShow"/>


    <detail-bottom-bar @addCart="addToCart"/>

  </div>
</template>

<script>

  import DetailNavBar from './childComps/DetailNavBar';
  import DetailInfo from './childComps/DetailInfo';
  import DetailShop from './childComps/DetailShop';
  import GoodsInfo from './childComps/GoodsInfo';
  import DetailParamsInfo from './childComps/DetailParamsInfo';
  import DetailCommentInfo from './childComps/DetailCommentInfo';
  import GoodsList from '../../components/content/goods/GoodsList';
  import DetailBottomBar from './childComps/DetailBottomBar';
  import BackTop from '../../components/content/backTop/BackTop';

  import Scroll from '../../components/common/scroll/Scroll';

  import {getDetail, getRecommend, Goods, GoodsParam, Shop} from '../../network/detail';
  import {debounce} from '../../common/utils';
  import {itemListenerMixin} from '../../common/mixin';

  export default {
    name: 'Detail',
    data() {
      return {
        iid: null,
        topImages: [],
        goods: {},
        shop: {},
        isImgLoad: false,
        detailImages: [],
        detailInfo: {},
        paramInfo: {},
        itemParams: {},
        commentInfo: {},
        recommends: [],
        themeTopYs: [],
        currentIndex: 0,
        isShow: false
      };
    },

    mixins: [itemListenerMixin],

    created() {
      this.iid = this.$route.params.iid;

      getDetail(this.iid).then((res) => {

        this.topImages = res.result.itemInfo.topImages;

        //获取商品信息
        this.goods = new Goods(res.result.itemInfo, res.result.columns, res.result.shopInfo.services);

        this.shop = new Shop(res.result.shopInfo);

        this.detailInfo = res.result.detailInfo;
        console.log(555,this.goods)

        this.paramInfo = new GoodsParam(res.result.itemParams.info, res.result.itemParams.rule);

        this.itemParams = res.result.itemParams;

        if (res.result.rate.list) {
          let a = 0;
          this.commentInfo = res.result.rate.list[a];
        }

        this.$nextTick(() => {

        });


      });

      getRecommend().then((res) => {
        console.log(res.data);
        this.recommends = res.data.list;
      });
    },

    components: {
      BackTop,
      DetailBottomBar,
      GoodsList,
      DetailCommentInfo,
      DetailParamsInfo,
      GoodsInfo,
      Scroll,
      DetailShop,
      DetailInfo,
      DetailNavBar
    },
    methods: {
      contentScroll(position) {

        //判断backTop是否显示
        this.isShow = position.y < -1000;
        // this.saveY = position.y;

        //判断tab-control是否吸顶
        // this.isTabFixed = (-position.y) > this.tabOffsetTop;

        //获取Y值
        const positionY = position.y;
        let length = this.themeTopYs.length;

        //position中的y和值进行对比
        for (let i = 0; i < length - 1; i++) {
          // if ((i < length - 1 && positionY < this.themeTopYs[i] && positionY > this.themeTopYs[i + 1])
          //   || (i === length - 1 && positionY < this.themeTopYs[i])) {
          //   console.log(i);
          // }

          // if (this.currentIndex !== i && ((i < length - 1 && positionY < this.themeTopYs[i] && positionY > this.themeTopYs[i + 1])
          //   || (i === length - 1 && positionY < this.themeTopYs[i]))) {
          //   this.currentIndex = i;
          //   this.$refs.detailNav.currentIndex = this.currentIndex;
          //   //console.log(i);
          // }

          if (this.currentIndex !== i && (positionY <= this.themeTopYs[i] && positionY > this.themeTopYs[i + 1])) {
            this.currentIndex = i;
            this.$refs.detailNav.currentIndex = this.currentIndex;
          }
        }
        // console.log(333, positionY);
        // console.log(444, this.themeTopYs);

      },


      carLoad() {
        this.$bus.$emit('imgLoad');
      },
      titleClick(index) {
        this.$refs.scroll.scrollTo(0, this.themeTopYs[index]);

      },

      backClick() {
        this.$refs.scroll.scrollTo(0, 0);
      },

      goodsImgLoad() {
        // this.$bus.$emit('goodsImgLoad');
        const refresh = debounce(this.$refs.scroll.refresh, 500);
        this.$bus.$on('detailItemImgLoad', () => {
          refresh();
          // this.$refs.scroll.refresh();
          // console.log(9999,this.$refs.scroll.scroll);

        });

        // refresh();

        this.themeTopYs = [];

        this.themeTopYs.push(0);
        this.themeTopYs.push(-this.$refs.params.$el.offsetTop + 30);
        this.themeTopYs.push(-this.$refs.comment.$el.offsetTop + 30);
        this.themeTopYs.push(-this.$refs.recommend.$el.offsetTop + 30);
        this.themeTopYs.push(-Number.MAX_VALUE);

        // console.log(687, this.themeTopYs);
      },

      addToCart() {
        const product = {};
        product.image = this.topImages[0];
        product.title = this.goods.title;
        product.desc = this.goods.desc;
        product.price = this.goods.newPrice;
        product.iid = this.iid;
        product.checked = true;
        console.log(8888,product);

        // this.$store.commit('addCart', product);
        this.$store.dispatch('addCart', product);
      }
    },

    updated() {
      this.$refs.scroll.refresh();


    },


    mounted() {
      const refresh = debounce(this.$refs.scroll.refresh, 500);
      this.$bus.$on('imgLoad', () => {
        refresh();
        // this.$refs.scroll.refresh();
        // console.log(this.$refs.scroll.scroll)
      });

      this.$bus.$on('goodsImgLoad', () => {
        refresh();
        // this.$refs.scroll.refresh();
        console.log(this.$refs.scroll.scroll);

      });

      this.$bus.$on('detailItemImgLoad', () => {
        refresh();
        // this.$refs.scroll.refresh();
        console.log(this.$refs.scroll.scroll);

      });


    }
  };
</script>

<style scoped>

  .detail {
    position: relative;
    z-index: 100;
    background-color: #ffffff;
    height: 100vh;

  }

  .ant-carousel >>> .slick-slide {
    text-align: center;
    height: 300px;
    line-height: 300px;
    overflow: hidden;
  }

  .ant-carousel >>> .slick-slide h3 {
    color: #fff;
  }

  .detail-content {
    height: calc(100% - 44px - 49px);
    overflow: hidden;
  }

  .white {
    height: 80px;
    width: 100%;
    background-color: #fff;
  }


</style>
